var t = getApp(), e = void 0;

Page({
    data: {
        myLocation: {
            latitude: 23.099994,
            longitude: 113.32452
        },
        serviceOptionsFlag: 0,
        serviceOptions: [ {
            label: "帮我送",
            startAddressNoticeText: "到哪里拿货",
            endAddressNoticeText: "送到哪里",
            editStartAddressTitle: "拿货",
            editEndAddressTitle: "收货"
        }, {
            label: "帮我取",
            startAddressNoticeText: "到哪里取货",
            endAddressNoticeText: "送到哪里",
            editStartAddressTitle: "取货",
            editEndAddressTitle: "收货"
        }, {
            label: "帮我买",
            startAddressNoticeText: "就近3公里购买",
            endAddressNoticeText: "送到哪里",
            editStartAddressTitle: "买货",
            editEndAddressTitle: "收货"
        }, {
            label: "代驾",
            startAddressNoticeText: "去哪儿接驾",
            endAddressNoticeText: "你要去哪儿",
            editStartAddressTitle: "接驾",
            editEndAddressTitle: "终点"
        } ],
        tags: [ "文件", "化妆品", "家具", "宠物", "蛋糕", "鲜花", "海鲜", "易碎物品", "保鲜" ],
        weights: [],
        weightsFlag: -1,
        weightsTitle: "选择重量范围",
        startAddress: {},
        endAddress: {},
        tagBuyFlag: 1,
        textarea: "",
        coupon_show: !1,
        showCoupon: {},
        formId: [ "", "", "", "" ]
    },
    advertation: function() {
        var t = null;
        wx.createInterstitialAd && ((t = wx.createInterstitialAd({
            adUnitId: "adunit-5f82fdbfbebbaae7"
        })).onLoad(function() {}), t.onError(function(t) {}), t.onClose(function() {})), 
        t && t.show().catch(function(t) {
            console.error(t);
        });
    },
    onLoad: function(t) {
        (e = this).getServiceStatus(), console.log(t), t.scene && wx.setStorageSync("superior_id", t.scene), 
        t.wx_id && wx.setStorageSync("superior_id", t.wx_id);
    },
    onShow: function() {
        this.getLookCoupon();
    },
    closeCoupon: function() {
        e.setData({
            coupon_show: !1
        });
    },
    getLookCoupon: function() {
        t.com.post("wx/coupon/look", {}, function(t) {
            0 === t.errno && e.setData({
                coupon_show: !!t.data.id,
                showCoupon: t.data
            });
        });
    },
    nextStep: function() {
        t.isPhoneExist() && ((this.data.startAddress.city || -1 != this.data.serviceOptions[this.data.serviceOptionsFlag].startAddressNoticeText.indexOf("就近")) && this.data.endAddress.city ? -1 == this.data.weightsFlag && 3 != this.data.serviceOptionsFlag ? wx.showToast({
            title: "请选择物品重量",
            icon: "none"
        }) : "" == this.data.textarea && 3 != this.data.serviceOptionsFlag ? wx.showToast({
            title: "请输入物品内容",
            icon: "none"
        }) : wx.navigateTo({
            url: "/pages/publish/publish?data=" + JSON.stringify({
                startAddress: this.data.serviceOptions[this.data.serviceOptionsFlag].startAddressNoticeText.indexOf("就近") > -1 ? "nearby" : this.data.startAddress,
                endAddress: this.data.endAddress,
                textarea: this.data.textarea,
                weight: this.data.weights[this.data.weightsFlag],
                title: this.data.serviceOptions[this.data.serviceOptionsFlag].label,
                formId: this.data.formId
            })
        }) : wx.showToast({
            title: "请选择地址",
            icon: "none"
        }));
    },
    getAddress: function(s) {
        if (t.isPhoneExist()) {
            var a = s.currentTarget.dataset.type;
            wx.navigateTo({
                url: "/pages/address/supply/supply?type=" + a + "&text=" + ("start" == a ? e.data.serviceOptions[e.data.serviceOptionsFlag].editStartAddressTitle : e.data.serviceOptions[e.data.serviceOptionsFlag].editEndAddressTitle) + "&address=" + JSON.stringify("start" == a ? this.data.startAddress : this.data.endAddress)
            });
        }
    },
    tagBuy: function(s) {
        if (t.isPhoneExist()) {
            var a = s.currentTarget.dataset.index;
            e.setData({
                "serviceOptions[2].startAddressNoticeText": 0 == a ? "去哪儿买" : "就近3公里购买",
                tagBuyFlag: a
            });
        }
    },
    changeService: function(t) {
        var s = t.currentTarget.dataset.index;
        e.setData({
            serviceOptionsFlag: s,
            startAddress: {},
            endAddress: {},
            weightsFlag: -1,
            textarea: ""
        }), e.initMyLocation();
    },
    weightsChange: function(t) {
        e.setData({
            weightsFlag: t.detail.value,
            weightsTitle: e.data.weights[t.detail.value].des
        });
    },
    initMyLocation: function() {
        wx.getLocation({
            type: "gcj02",
            success: function(s) {
                t.com.post("wx/location/geocoder", {
                    location: s.latitude + "," + s.longitude
                }, function(t) {
                    0 === t.errno && (wx.setStorageSync("city_name", t.data.address_component.city), 
                    wx.setStorageSync("getLocation", t.data.address_component.city), e.getAgentConfig(), 
                    0 == e.data.serviceOptionsFlag || 3 == e.data.serviceOptionsFlag ? e.setData({
                        "startAddress.street_number": t.data.address_component.street_number,
                        "startAddress.formatted_addresse": t.data.formatted_addresses.recommend,
                        "startAddress.province": t.data.address_component.province,
                        "startAddress.city": t.data.address_component.city,
                        "startAddress.district": t.data.address_component.district,
                        "startAddress.phone": wx.getStorageSync("phone"),
                        "startAddress.latitude": s.latitude,
                        "startAddress.longitude": s.longitude
                    }) : e.setData({
                        "endAddress.street_number": t.data.address_component.street_number,
                        "endAddress.formatted_addresse": t.data.formatted_addresses.recommend,
                        "endAddress.province": t.data.address_component.province,
                        "endAddress.city": t.data.address_component.city,
                        "endAddress.phone": wx.getStorageSync("phone"),
                        "endAddress.district": t.data.address_component.district,
                        "endAddress.latitude": s.latitude,
                        "endAddress.longitude": s.longitude
                    }));
                });
            }
        });
    },
    getServiceStatus: function() {
        wx.showLoading({
            title: "获取服务状态中"
        }), t.com.post("wx/config/service", {}, function(t) {
            if (wx.hideLoading(), 0 == t.errno && JSON.parse(t.data.config_content)) {
                var s = JSON.parse(t.data.config_content), a = e.data.serviceOptions;
                a[0].status = s.send_service, a[0].time = s.init_send_time, a[1].status = s.take_service, 
                a[1].time = s.init_take_time, a[2].status = s.buy_service, a[2].time = s.init_buy_time, 
                a[3].status = s.drive_service, a[3].time = s.init_drive_time, a[2].startAddressNoticeText = "就近" + s.buy_meter + "公里购买", 
                e.setData({
                    open_agent: s.open_agent,
                    serviceOptions: a,
                    serviceStatus: t.data.config_content ? JSON.parse(t.data.config_content) : ""
                }), e.initMyLocation();
            }
        });
    },
    getAgentConfig: function() {
        t.com.post("wx/agent/config", {
            city_name: 1 == this.data.open_agent ? wx.getStorageSync("city_name") : "全国"
        }, function(t) {
            0 === t.errno && (wx.setStorageSync("agent", t.data), e.initSerice(t.data));
        });
    },
    initSerice: function(t) {
        for (var s in t.weight) t.weight[s].des = (t.weight[s].min_weight / 1e3).toFixed(0) + "~" + (t.weight[s].max_weight / 1e3).toFixed(0) + "kg";
        var a = this.data.serviceOptions;
        for (var i in t.service) for (var d in a) t.service[i].service_type == a[d].label && (t.service[d].des_tags && (a[d].tags = t.service[i].des_tags.split(",")), 
        a[d].time = t.service[i].init_time, a[d].status = t.service[i].open_service, "帮我买" == t.service[i].service_type && (a[d].startAddressNoticeText = "就近" + t.service[i].buy_meter + "公里购买"));
        e.setData({
            weights: t.weight,
            serviceOptions: a
        });
    },
    textInput: function(t) {
        this.setData({
            textarea: t.detail.value
        });
    },
    clickTag: function(t) {
        this.setData({
            textarea: this.data.textarea + " " + t.currentTarget.dataset.tag
        });
    },
    wsubmit: function(t) {
        this.setData({
            "formId[0]": t.detail.formId
        });
    },
    desform: function(t) {
        this.setData({
            "formId[1]": t.detail.formId
        });
    },
    nextform: function(t) {
        this.setData({
            "formId[2]": t.detail.formId
        });
    },
    onShareAppMessage: function() {
        var t = wx.getStorageSync("wxapp");
        return {
            title: t.shareTitle,
            path: t.sharePath + "?wx_id=" + wx.getStorageSync("wx_id")
        };
    }
});